package com.example.translateappjava.Activity.MineFragmentActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.translateappjava.Activity.KnowledgeFragmentActivity.NewsDetailsActivity;
import com.example.translateappjava.Adapter.NewsInfo;
import com.example.translateappjava.Adapter.NewsListAdapter;
import com.example.translateappjava.DataBase.CollectDbHelper;
import com.example.translateappjava.DataBase.CollectInfo;
import com.example.translateappjava.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class MyCollectionsActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private NewsListAdapter mNewsListAdapter;
    private List<NewsInfo.ResultBean.DataBean> mDataBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_my_collections);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        applyColorSettings();

        recyclerView = findViewById(R.id.recyclerView);

        mNewsListAdapter = new NewsListAdapter(this);
        recyclerView.setAdapter(mNewsListAdapter);

        List<CollectInfo> collectInfoList = CollectDbHelper.getInstance(MyCollectionsActivity.this).queryCollectListData("NULL");
        Gson gson = new Gson();
        for(int i = 0;i<collectInfoList.size();i++){
            mDataBeanList.add(gson.fromJson(collectInfoList.get(i).getNew_json(),NewsInfo.ResultBean.DataBean.class));
        }

        mNewsListAdapter.setListData(mDataBeanList);
        mNewsListAdapter.setMonItemClickListener(new NewsListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(NewsInfo.ResultBean.DataBean dataBean, int positon) {
                Intent intent = new Intent(MyCollectionsActivity.this, NewsDetailsActivity.class);
                intent.putExtra("dataBean",dataBean);
                startActivity(intent);
            }
        });


        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        applyColorSettings(); // 再次调用 applyColorSettings() 以确保颜色设置
    }
    private void applyColorSettings() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        int defaultColor = getResources().getColor(R.color.toolbar_color); // 默认颜色
        int color = sharedPreferences.getInt("color", defaultColor); // 如果未找到颜色，则使用默认颜色
        Toolbar toolbar = findViewById(R.id.toolbar);

        // 应用颜色到您想要更改的视图
        toolbar.setBackgroundColor(color);

        // 如果有其他视图也需要应用颜色，也可以在这里设置
    }

}